package com.kakarote.crm9.erp.jxc.controller;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Record;
import com.kakarote.crm9.common.config.paragetter.BasePageRequest;
import com.kakarote.crm9.erp.jxc.entity.GoodsSuppliers;
import com.kakarote.crm9.erp.jxc.service.GoodsSuppliersService;
import com.kakarote.crm9.erp.work.entity.Work;
import com.kakarote.crm9.utils.PageData;
import com.kakarote.crm9.utils.R;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @date ：Created in 2021-03-16 9:58
 * @description：供应商控制器
 * @modified By：`
 * @version: 1.0
 */
public class GoodsSuppliersConteoller extends Controller {

    @Inject
    private GoodsSuppliersService suppliersService;


    public void list( ){
        List<Record> list = suppliersService.list(getpageData());
        renderJson(R.ok().put("data",list).put("total",list.size()));
    }

    /**
     * 获取保供应商详细详细信息
     */
    public void getInfo(){
        if(getInt("id")!=null){
            renderJson(R.ok().put("data",suppliersService.getInfo(getInt("id"))));
        }else {
            renderJson(R.error("请求参数不全"));
        }
    }
    /**
     * 新增供应商
     */
    public void add(){
        renderJson(suppliersService.add(getpageData()));
    }

    /**
     * 修改供应商信息
     */
    public void edit(){
        renderJson(suppliersService.edit(getpageData()));
    }

    /**
     * 删除供应商
     */
    public void remove(@Para("ids") String ids )
    {
        if (ids == null || ids.length() <= 0) renderJson(R.error());
        List<String> list = new ArrayList();
        if(ids.contains(",")){
            String[] split = ids.split(",");
            for (int i = 0; i < split.length; i++) {
                if(split[i]!=null&&!"".equals(split[i])&&Integer.parseInt(split[i])>0){
                    System.out.println("i = " + i);
                    list.add(split[i]);
                }
            }
            renderJson(suppliersService.remove(list));
        }else{
            list.add(ids);
            renderJson(suppliersService.remove(list));
        }

    }
    //获取请求数据
    private PageData getpageData(){
        return new PageData(this.getRequest());
    }
}
